<?php declare(strict_types=1);


namespace App\Rpc\Service;

use App\Rpc\Lib\OrderInterface;
use Swoft\Redis\Pool;
use Swoft\Rpc\Server\Annotation\Mapping\Service;

/**
 * Class OrderService
 *
 * @since 2.0
 *
 * @Service()
 */
class OrderService implements OrderInterface
{
    /**
     * 更新订单状态
     * @param $data
     * @return array
     * @throws \ReflectionException
     * @throws \Swoft\Bean\Exception\ContainerException
     */
    public function update($data): array
    {
        var_dump("订单更新成功", $data);
        //业务执行成功
        $connectionRedis = bean(Pool::DEFAULT_POOL)->getConnection();
        $connectionRedis->setRelease(true);
        //调用mysql数据库更新积分

        #确认某个任务执行成功
        if(!empty($connectionRedis->hset('order_message_job',(string)$data['msg_id'],1))){
            $result =  ['status' => 1,'result' => '订单状态更新成功'];
        }else{
            $result =  ['status' => 0,'result' => '订单状态更新失败'];
        }
        $connectionRedis->release();
        return $result;
    }

    /**
     * 查询某个任务的状态
     * @param $msg_id
     * @return array
     */
    public function confirmStatus($msg_id):array
    {
        var_dump('消息id:'.$msg_id);
        $connectionRedis = bean(Pool::DEFAULT_POOL)->getConnection();
        $connectionRedis->setRelease(true);
        //确认某个任务执行成功
        if(!empty($connectionRedis->hget('order_message_job',(string)$msg_id))){
            $result = ['status' => 1 ,'result' => '订单状态任务执行成功'];
        }else{
            $result = ['status' => 0 ,'result' => '订单状态任务执行失败'];
        }
        $connectionRedis->release();
        return $result;
    }

    /**
     * 尝试提交（第一阶段try）
     * @param $context
     * @return array
     */
    public function creditOrderTcc($context):array
    {
        var_dump('order第一阶段try');
        return ['status' => 0 ,'result' => 'order第一阶段try'];
    }


    /**
     * 业务提交执行（第二阶段Confirm）
     * @param $context
     * @return array
     */
    public function confirmCreditOrderTcc($context):array
    {
        var_dump('order第二阶段Confirm');
        return ['status' => 1 ,'result' => 'order第二阶段Confirm'];
    }


    /**
     * 回滚（第二阶段cancel）
     * @param $context
     * @return array
     */
    public function cancelCreditOrderTcc($context):array
    {
        var_dump('order第二阶段cancel');
        return ['status' => 1 ,'result' => 'order第二阶段cancel'];
    }

}